<?php

class Itvina_Controller_Action_Helper_Time extends Zend_Controller_Action_Helper_Abstract
{
	public $pluginLoader;

	public function __construct()
	{
		$this->pluginLoader = new Zend_Loader_PluginLoader ();
	}

	function direct($function, $params = array())
	{
		return $function ( func_get_args () );
	}
	
	function first($value,$format_time="d.m.Y")
	{
		$value=Itvina_Model_Abstract::settype($value,Itvina_Model_Abstract::TIME );		
		$format_time=str_replace(array("-",".","/"," "), "", $format_time);
		$format_time=strtolower($format_time);
		if($format_time=="dmy")
		{
			return mktime(0,0,0,date('m',$value),date('d',$value),date('Y',$value));
		}
		else if($format_time=="hidmy")
		{
			return mktime(date('H',$value),date('i',$value),0,date('m',$value),date('d',$value),date('Y',$value));
		}
		else if($format_time=="my")
		{
			return mktime(0,0,0,date('m',$value),1,date('Y',$value));
		}
		else if($format_time=="y")
		{
			return mktime(0,0,0,1,1,date('Y',$value));	
		}
	}
	
	function last($value,$format_time="d.m.Y")
	{
		$value=Itvina_Model_Abstract::settype($value,Itvina_Model_Abstract::TIME );
		$format_time=str_replace(array("-",".","/"," "), "", $format_time);
		$format_time=strtolower($format_time);
		if($format_time=="dmy")
		{
			return mktime(23,59,59,date('m',$value),date('d',$value),date('Y',$value));
		}
		else if($format_time=="hidmy")
		{
			return mktime(date('H',$value),date('i',$value),59,date('m',$value),date('d',$value),date('Y',$value));
		}
		else if($format_time=="my")
		{
			$days_in_month=cal_days_in_month(CAL_GREGORIAN, date('m',$value),date('m',$value)); 
			return mktime(23,59,59,date('m',$value),$days_in_month,date('Y',$value));
		}
		else if($format_time=="y")
		{
			$days_in_month=cal_days_in_month(CAL_GREGORIAN, date('m',$value),date('m',$value));
			return mktime(23,59,59,12,$days_in_month,date('Y',$value));	
		}
	}
}
